package com.lq.ecoldchain.ui.main.introduce

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.common.PhotoAddRecyclerAdapter
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.toast

/*
*2019/1/6 0006  17:21
*function by lq
*/
class IntroductionViewModel : BaseViewModel() {
    val publishText by lazy { MutableLiveData<String>() }
    val list by lazy { ArrayList<String>() }
    val adapter by lazy { PhotoAddRecyclerAdapter(list) }
    val startPlace by lazy { MutableLiveData<String>() }
    val startId by lazy { MutableLiveData<Int>() }
    val endPlace by lazy { MutableLiveData<String>() }
    val endId by lazy { MutableLiveData<Int>() }

    val carTypeName by lazy { MutableLiveData<String>() }
    val carTypeId by lazy { MutableLiveData<String>() }
    val type by lazy { MutableLiveData<String>() }
    val typeName by lazy { MutableLiveData<String>() }
    val price by lazy { MutableLiveData<String>() }
    val person by lazy { MutableLiveData<String>() }
    val way by lazy { MutableLiveData<String>() }
    val free by lazy { MutableLiveData<String>() }
    val address by lazy { MutableLiveData<String>() }
    val isGreen by lazy { MutableLiveData<Boolean>() }
    val isMeatHook by lazy { MutableLiveData<Boolean>() }
    val isTailstock by lazy { MutableLiveData<Boolean>() }

    val map by lazy { HashMap<String, Any>() }

    init {
        isGreen.value = true
        isMeatHook.value = true
        isTailstock.value = true
    }

    fun commit() {
        if (publishText.value.isNullOrEmpty() || startId.value == null || endId.value == null || person.value.isNullOrEmpty() || carTypeId.value.isNullOrEmpty() ||
                way.value.isNullOrEmpty() || price.value.isNullOrEmpty() || type.value.isNullOrEmpty() || address.value.isNullOrEmpty() || publishText.value.isNullOrEmpty()
                || free.value.isNullOrEmpty()) {
            toast { "请填写完整信息" }
        } else {
            map.clear()
            map["startCityId"] = startId.value!!
            map["endCityId"] = endId.value!!
            map["contacts"] = person.value!!//联系人
            map["vehicleTypeId"] = carTypeId.value!!//车型
            map["contactWay"] = way.value!!
            map["quote"] = price.value!!.toInt()
            map["goodsTypeId"] = type.value!!//商品类型
            map["isGreenPass"] = isGreen.value!!//绿色通道
            map["isMeatHook"] = isMeatHook.value!!//肉购车
            map["isTailstock"] = isTailstock.value!!//尾班车
            map["explainVoice"] = address.value!!//语言描述地址
            map["explain"] = publishText.value!!//文字描述
            map["introduceFee"] = free.value!!.toInt()//介绍费
            BaseNetCore.ApiServiceImpl.submitIntroduce(map).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
                override fun onSuccess(t: String?) {
                    toast { "提交成功" }
                }

                override fun onFail(code: String) {}
            })
        }
    }


}